// pages/notice/notice.js
const app = getApp()
const util = require('../../utils/util.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
          num:"0",
          title: "收入排行",//当前页面的标题
          listshow:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
          this.setData({
                  headerheight: app.globalData.navHeight,
                  contenheight: app.globalData.screenHeight - app.globalData.navHeight
          })
                  this.request()
  },
//   跳转到邀请好友
        invite:function(){
                wx.navigateTo({
                        url: '/pages/invite/invite',
                })
        },
        // 登录
        login: function () {
                var that = this
                wx.login({
                        success: function (res) {
                                var code = res.code
                                var data = {
                                        userCode: code,
                                        userImageUrl: "",
                                        userName: ""
                                }
                                util.promotion(app.globalData.appurl + "/qinqiang/user", "POST", data, that.succeed)
                        }
                })
        },
        succeed: function (res) {
                wx.setStorageSync('tekon', res.data.token)
                wx.setStorageSync('userInfo', res.data.user)
                this.request()
        },
        request: function () {
                wx.showNavigationBarLoading()
                wx.showLoading({
                        title: '玩命加载中...',
                })
                var data={
                        user_id:wx.getStorageSync('userInfo').id
                }
                var header = {
                        'authorization': wx.getStorageSync('tekon'),
                        'content-type': "application/x-www-form-urlencoded"
                }
                if (this.data.num == 0) {
                        //好友排行
                        util.formhttp(app.globalData.appurl + "/goldconins/week-top", 'get', data, header, this.stat);
                } else {   
                        //总排行
                        util.formhttp(app.globalData.appurl + "/goldconins/total-top", 'get', null, header, this.stat);
                }
               
        },
        stat: function (res) {
                if (res.status == 401) {
                        this.login()
                }
                wx: wx.hideNavigationBarLoading()
                wx: wx.hideLoading()
                var list = [];
                for (var ids in res.data.list) {
                        var detail = res.data.list[ids];
                        var temp = {
                                coins: detail.coins,
                                user_name: detail.user_name
                        }
                        list.push(temp)
                }
              
                if (res.data.list.length==0){
                        this.setData({
                                listshow: true
                        })
                }else{
                        this.setData({
                                listshow: false
                        })
                }
                this.setData({
                        list: list,
                })

        },
        choosesorta:function(e){
                this.setData({
                        list:[],
                        num: e.currentTarget.dataset.num
                })
                this.request()
        }
})